function ViewMVC (_name,_element,_transform,_beforinvoke,_afterinvoke)   
{   
    this.name=_name||'Untitled View';   
    this.element=_element;   
    this.invoke = function ()   
    {   
        if (! this.element) return false;   
        var dx=getElement (this.element);   
        if (! dx) return false;   
        if (!this.transform) {dx.innerHTML='Transformer not found for ' + this.name + '!';return false; }   
        this.beforinvoke();   
		dx.style.display = 'inline';
        dx.innerHTML = this.transform();   
        this.afterinvoke();    
        return this.name;  
    }  
    this.beforinvoke = _beforinvoke || function () {}    
    this.afterinvoke = _afterinvoke || function () {}    
    this.transform = _transform || function () { return this.name + ' has not been initialized.'; }   
}   

function EventMVC (_name)   
{   
    this.name=_name||'Untitled Event';   
    this.views=[];    
    if (!Controller.mvcevents)Controller.mvcevents=[]; 
    Controller.mvcevents[this.name]=this;   
    this.invoke = function ()   
    {   
        window.mvcevent = this.name;   
        for (var v in this.views)   
        {   
            this.views[v].invoke();   
        }   
        this.afterinvoke();  
		attachWaitInvoke () ;
        return this.name;  
    }    
    this.add = function () { var x=this.add.arguments; for (var i=0;i<x.length;i++) this.views.push(x[i]); }   
    this.afterinvoke = function () {}    
}


function CustomAJAXBatchConnector ()
{   
    this.array = null;
    this.send = function (array)
    {
	    var connection;
        this.array = array;
        this.count = array.length;
        for (var x in array)
        {
            array[x].aftersend = function () {  window.connector.next(); }
            connection = CustomAJAXConnector.create();
            connection.batch (array[x]);
        }
        window.isbusy = true;
        window.disabler.invoke (window.isbusy); 
    }
    this.next = function ()
    {
	    this.count --
        if (this.count==0) this.oncomplete ();
    }
    this.oncomplete = function () { }
}



function CustomAJAXConnector (target, aftersend, rowname, columnname, onreceive)
{  
	this.AJAX = null;
	this.query = [];
	if (!window.connections) window.connections=[];
	this.id = window.connections.length;
	window.connections[this.id]=this;
	
    this.load = function (target, aftersend, rowname, columnname, onreceive)
    {
        this.target         = target || [];
        this.aftersend      = aftersend || function () { };
        this.rowname        = rowname || 'record';
        this.columnname     = columnname || 'name';
        this.onreceive      = onreceive || function () { };
        this.method         = 'GET';
        this.postdata       = null;
        this.question       = null;
        this.serializer     = null;
        this.beforesend     = function () { };  
        this.message        = "Connected. Waiting for response...";
        this.params         =  { 'action' : 'next5' }; 
    }
    
    this.clone = function (object)
    {
        this.load ();
        for (var x in object) this[x] = object[x];
    }
    
    this.create = function (object)
    {
        this.clone(object);
        this.send();
    }
    
    this.batch = function (object)
    { 
        this.clone(object);
        this.AJAX = new ajaxObject(this.createURL(), function (text, id) { window.connections[id].serialize(text); }, this.message);
        this.AJAX.update (this.id);
    }
    
    this.send = function (params)
    { 
        if (params) this.params = params;
        this.beforesend(); 
        if (!this.question || doconfirm (this.question)) 
        {
            if (this.method=='GET') wsdl (this.createURL(), function () { window.request.serialize(); }, this.message);   
            else wsdl(DATA_URL, function () { window.request.serialize(); }, this.message, this.postdata ) ;
        }
    }
    
    this.serialize = function (response)
    {
        var http = response ? this.AJAX.AJAX : window.http; 
        if (http.readyState==ReadyState.DONE)   
        {
            var text = response || window.http.responseText; //confirm (text)
            if (window.dom.loadXML (text))  
            {  
                var si=new SimpleXMLIterator (window.dom, this.rowname, this.columnname);   
                if (!this.columnname) si.fieldname=null;
                while (!si.eof)  
                {      
                    if (this.serializer) this.serializer ( this.target, si.data );
                    else if (this.target) this.target.push (si.data);
                    si.movenext ();  
                }   
                this.serializer = null;  
                // this.SaveSQLforTesting ();   
            }  
            if (! response ) Controller.Clear(); 



            try { if (this.aftersend) this.aftersend (text); } catch (ex) { } 
        } 
        this.onreceive ();     
    }
    
    this.createURL = function ()
    {
        var url=[];  
        for (var x in this.params) url.push (x + '/' + this.params[x]);
		this.query.push ( { 'action' : this.params['action'] || 'Untitled query', 'url' :  DATA_URL + url.join ('/') } );
        return DATA_URL + url.join ('/');
    }
    
    this.SaveSQLforTesting = function ()
    {
        var si=new SimpleXMLIterator (window.dom, 'sql');   
        si.fieldname=null; 
        while (!si.eof)  
        {      
            this.sql.push (si.data);
            si.movenext ();  
        } 
		TOTALBYTES += window.http.responseText.length;  
    }
	this.sql = [];
    this.load (target, aftersend, rowname, columnname);
}
 
CustomAJAXConnector.create = function () { return new CustomAJAXConnector(); } 
CustomAJAXBatchConnector.create = function () { return new CustomAJAXBatchConnector(); }
window.connector = CustomAJAXBatchConnector.create ();
